package com.card.server.controller;

import com.card.server.domain.R;
import com.card.server.domain.RoomEmitters;
import com.card.server.domain.UserEmitters;
import com.card.server.service.IRoomService;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author TISNIW
 * @version 1.0
 * @date 2025/5/26 16:46
 * @desc
 */
@RestController
@RequestMapping("/room")
public class RoomController {
    private static final Logger log = LoggerFactory.getLogger(RoomController.class);
    @Autowired
    private IRoomService roomService;
    /**
     * 用户进入房间
     */
    @RequestMapping("/enter")
    public R<String> enter(Integer userId, String roomkey) {
        return roomService.enter(userId, roomkey);
    }
    @GetMapping(value = "/start", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> send(@RequestParam String room,Integer userId ,HttpServletRequest request){

        return Flux.create(emitter -> {
            RoomEmitters.roomEmitters.put(room.toString(), emitter);
            // 连接关闭时移除
            emitter.onDispose(() -> UserEmitters.userEmitters.remove(room.toString()));
        });
    }
}
